package com.sdry.web.controller.lz;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.lz.CheckItem;
import com.sdry.model.lz.CheckItemDetail;
import com.sdry.model.lz.DeviceCheckItem;
import com.sdry.model.lz.LzQueryCriteria;
import com.sdry.service.lz.DeviceAlarmService;
import com.sdry.utils.ResponseUtil;
import com.sdry.utils.StringUtil;

/**
 * @ClassName DeviceAlarmController
 * @Description 设备巡检记录
 * @Author lz
 * @Date 2018年10月8日 18:02:42
 * @Version 1.0
 */
@Controller
@RequestMapping("/deviceAlarm")
public class DeviceAlarmController {

    public static final Integer AFFECT_ROW = 0;
    @Resource
    DeviceAlarmService deviceAlarmService;

    /**
     * 添加设备巡检记录表
     * @param response
     * @param deviceCheckItem 巡检实体
     * @throws Exception
     * @RequestParam("map") Map<String, HashSet<String>> checkItemDetailIDArr
     */
    @RequestMapping("/addCheckRecord")
    public void addCheckRecord(HttpServletResponse response, @RequestBody DeviceCheckItem deviceCheckItem) throws Exception {
    	if (deviceCheckItem != null) {
			if(StringUtil.isEmpty(deviceCheckItem.getSubmitTime())){
				deviceCheckItem.setSubmitTime(null);
			}
		}
        Long affectRow = deviceAlarmService.addCheckRecord(deviceCheckItem);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 单个删除和批量删除
     * @param response
     * @param idArr id数组
     * @throws Exception
     */
    @RequestMapping("/deleteCheckRecordById")
    public void deleteCheckRecordById(HttpServletResponse response, @RequestParam("idArr[]") String[] idArr) throws Exception {
        for (int i = 0; i < idArr.length; i++) {
            Long affectRow = deviceAlarmService.deleteCheckRecordById(Long.parseLong(idArr[i]));
            if (affectRow > AFFECT_ROW) {
                ResponseUtil.write(response, affectRow);
            } else {
                ResponseUtil.write(response, AFFECT_ROW);
            }
        }
    }

    /**
     * 根据ID修改巡检记录表信息
     * @param response
     * @param deviceCheckItem 选中行的巡检项
     * @throws Exception
     */
    @RequestMapping("/editCheckRecordById")
    public void editCheckRecordById(HttpServletResponse response, DeviceCheckItem deviceCheckItem) throws Exception {
    	if (deviceCheckItem != null) {
			if(StringUtil.isEmpty(deviceCheckItem.getSubmitTime())){
				deviceCheckItem.setSubmitTime(null);
			}
		}
        Long affectRow = deviceAlarmService.editCheckRecordById(deviceCheckItem);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 分页查询巡检记录表
     * @param response
     * @param criteria 分页查询条件
     */
    @RequestMapping("/queryCheckRecord")
    public void queryCheckRecord(HttpServletResponse response, LzQueryCriteria criteria){
        String keyword01 = criteria.getKeyword01();
        String keyword02 = criteria.getKeyword02();
        if(keyword01 == null){
            keyword01 = "";
        }
        if(keyword02 == null){
            keyword02 = "";
        }
        criteria.setKeyword01(keyword01);
        criteria.setKeyword02(keyword02);
        List<DeviceCheckItem> deviceCheckItemsList = deviceAlarmService.queryCheckRecord(criteria);
        int count = deviceAlarmService.countCheckRecord(criteria);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(deviceCheckItemsList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        jsonObject.put("count", count);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加设备巡检项
     * @param response
     * @param checkItem
     * @throws Exception
     */
    @RequestMapping("/addCheckItem")
    public void addCheckItem(HttpServletResponse response, CheckItem checkItem) throws Exception {
        Long affectRow = deviceAlarmService.addCheckItem(checkItem);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 分页查询巡检项
     * @param response
     * @param criteria 分页查询条件
     */
    @RequestMapping("/queryCheckItem")
    public void queryCheckItem(HttpServletResponse response, LzQueryCriteria criteria){
        String keyword01 = criteria.getKeyword01();
        String keyword02 = criteria.getKeyword02();
        if(keyword01 == null){
            keyword01 = "";
        }
        if(keyword02 == null){
            keyword02 = "";
        }
        criteria.setKeyword01(keyword01);
        criteria.setKeyword02(keyword02);
        List<CheckItem> checkItemList = deviceAlarmService.queryCheckItem(criteria);
        int count = deviceAlarmService.countCheckItem(criteria);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(checkItemList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        jsonObject.put("count", count);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 单个删除和批量删除
     * @param response
     * @param idArr id数组
     * @throws Exception
     */
    @RequestMapping("/deleteCheckItemById")
    public void deleteCheckItemById(HttpServletResponse response, @RequestParam("idArr[]") String[] idArr) throws Exception {
        for (int i = 0; i < idArr.length; i++) {
            Long affectRow = deviceAlarmService.deleteCheckItemById(Long.parseLong(idArr[i]));
            if (affectRow > AFFECT_ROW) {
                ResponseUtil.write(response, affectRow);
            } else {
                ResponseUtil.write(response, AFFECT_ROW);
            }
        }
    }

    /**
     * 根据ID修改巡检项信息
     * @param response
     * @param checkItem 选中行的巡检项
     * @throws Exception
     */
    @RequestMapping("/editCheckItemById")
    public void editCheckItemById(HttpServletResponse response, CheckItem checkItem) throws Exception {
        Long affectRow = deviceAlarmService.editCheckItemById(checkItem);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 增加设备巡检项详细
     * @param response
     * @param checkItemDetail
     * @throws Exception
     */
    @RequestMapping("/addCheckItemDetail")
    public void addCheckItemDetail(HttpServletResponse response, CheckItemDetail checkItemDetail) throws Exception {
        Long affectRow = deviceAlarmService.addCheckItemDetail(checkItemDetail);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }


    /**
     * 根据ID查询巡检项详细
     * @param response
     * @param cdId 选中行的巡检项
     * @throws Exception
     */
    @RequestMapping("/queryCheckItemDetailById")
    public void queryCheckItemDetailById(HttpServletResponse response, String cdId) throws Exception {
        List<CheckItemDetail> checkItemDetailList = deviceAlarmService.queryCheckItemDetailById(Long.parseLong(cdId));
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(checkItemDetailList);
        if (checkItemDetailList.size() > AFFECT_ROW){
            jsonObject.put("code", AFFECT_ROW);
            jsonObject.put("data", jsonArray);
            jsonObject.put("count", 10);
            ResponseUtil.write(response, jsonObject);
        } else {
            jsonObject.put("code", AFFECT_ROW);
            jsonObject.put("data", null);
            jsonObject.put("count", 10);
            ResponseUtil.write(response, jsonObject);
        }
    }

    /**
     * 根据选中行的ID删除该行数据
     * @param response
     * @param id 要删除行的ID
     * @throws Exception
     */
    @RequestMapping("/deleteCheckItemDetailById")
    public void deleteCheckItemDetailById(HttpServletResponse response, String id) throws Exception {
        Long affectRow = deviceAlarmService.deleteCheckItemDetailById(Long.parseLong(id));
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 根据选中行的ID编辑该行数据
     * @param response
     * @param checkItemDetail 修改实体
     * @throws Exception
     */
    @RequestMapping("/editCheckItemDetailById")
    public void editCheckItemDetailById(HttpServletResponse response, CheckItemDetail checkItemDetail) throws Exception {
        Long affectRow = deviceAlarmService.editCheckItemDetailById(checkItemDetail);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 根据ID查询巡检项
     * @param response
     * @param id 选中行的巡检项
     * @throws Exception
     */
    @RequestMapping("/queryCheckItemById")
    public void queryCheckItemById(HttpServletResponse response, String id) throws Exception {
        //根据该行id查询数组remark02
        DeviceCheckItem deviceCheckItem = deviceAlarmService.queryArrayListById(Long.parseLong(id));
        List<CheckItem> checkItemList = new ArrayList<CheckItem>();
        if (deviceCheckItem != null) {
            JSONObject jsonObject = JSONObject.fromObject(deviceCheckItem.getRemark02());
            Map<String, List<String>> mapObj = (Map)jsonObject;
            for (Map.Entry<String, List<String>> entry : mapObj.entrySet()) {
                //System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
                //根据查出的id遍历选项
                CheckItem checkItem = deviceAlarmService.queryCheckItemById(Long.parseLong(entry.getKey()));
                checkItemList.add(checkItem);
            }
        }
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(checkItemList);
        if (checkItemList.size() > AFFECT_ROW){
            jsonObject.put("code", AFFECT_ROW);
            jsonObject.put("data", jsonArray);
            jsonObject.put("count", 10);
            ResponseUtil.write(response, jsonObject);
        } else {
            jsonObject.put("code", AFFECT_ROW);
            jsonObject.put("data", null);
            jsonObject.put("count", 10);
            ResponseUtil.write(response, jsonObject);
        }
    }

    /**
     * 根据ID查询巡检项详细
     * @param response
     * @param cdId
     * @throws Exception
     */
    @RequestMapping("/queryCheckItemDetailById3")
    public void queryCheckItemDetailById3(HttpServletResponse response, String preRowId, String cdId) throws Exception {
        List<CheckItemDetail> checkItemDetailList = new ArrayList<CheckItemDetail>();
        //根据该行id查询数组remark02
        DeviceCheckItem deviceCheckItem = deviceAlarmService.queryArrayListById(Long.parseLong(preRowId));
        if (deviceCheckItem != null) {
            JSONObject jsonObject = JSONObject.fromObject(deviceCheckItem.getRemark02());
            Map<String, List<Integer>> mapObj = (Map)jsonObject;
            for (Map.Entry<String, List<Integer>> entry : mapObj.entrySet()) {
                //System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
                //根据查出的id遍历选项,如果当前id与map的key匹配,则查询它的所有value
                if (entry.getKey().equals(cdId)) {
                    for (int i = 0; i < entry.getValue().size(); i++) {
                        //System.out.println(entry.getValue().get(i));
                        CheckItemDetail checkItemDetail1 = deviceAlarmService.queryCheckItemDetailListByList(entry.getValue().get(i));
                        checkItemDetailList.add(checkItemDetail1);
                    }
                    //System.out.println(entry.getValue().size());
                }
            }
        }
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(checkItemDetailList);
        if (checkItemDetailList.size() > AFFECT_ROW){
            jsonObject.put("code", AFFECT_ROW);
            jsonObject.put("data", jsonArray);
            jsonObject.put("count", 10);
            ResponseUtil.write(response, jsonObject);
        } else {
            jsonObject.put("code", AFFECT_ROW);
            jsonObject.put("data", null);
            jsonObject.put("count", 10);
            ResponseUtil.write(response, jsonObject);
        }
    }
}
